﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UcAsp.Net.Interface;
using Newtonsoft.Json;
using System.IO;
namespace TextEdit
{
    public partial class UcAspControl : UserControl, IExtract
    {
        private DateTime _adddatetime = DateTime.Now;
        private Editor.TextControl txtContent = new Editor.TextControl();
        public UcAspControl()
        {
            InitializeComponent();
        }
        #region 属性
        /// <summary>
        /// 常规项
        /// </summary>
        /// 
        public string Security { get; set; }
        public string Image { get; set; }
        public int ID { get; set; }
        public string Title { get; set; }
        public string Tag { get; set; }
        public string IconUrl { get; set; }
        public string ArticleContent { get; set; }
        public string MetaKeyword { get; set; }
        public string MetaDescrition { get; set; }
        public string Description { get; set; }
        public DateTime AddDatetime { get { return this._adddatetime; } set { this._adddatetime = value; } }
        public bool IsRelease { get; set; }
        public bool IsRecommend { get; set; }
        public int Hot { get; set; }
        public int Rate { get; set; }
        public int CategorieID { get; set; }
        public string ShowPath { get; set; }
        public string ViewsTemp { get; set; }
        public int UserID { get; set; }
        public int Rank { get; set; }

        /// <summary>
        /// 可变文字项
        /// </summary>
        public string Note1 { get; set; }
        public string Note2 { get; set; }
        public string Note3 { get; set; }
        public string Note4 { get; set; }
        public string Note5 { get; set; }
        public string Note6 { get; set; }
        public string Note7 { get; set; }
        public string Note8 { get; set; }
        public string Note9 { get; set; }
        /// <summary>
        /// 可变数组项
        /// </summary>
        public int Int1 { get; set; }
        public int Int2 { get; set; }
        public int Int3 { get; set; }
        public int Int4 { get; set; }
        public int Int5 { get; set; }


        public double Float1 { get; set; }
        public double Float2 { get; set; }
        public double Float3 { get; set; }
        public double Float4 { get; set; }
        public double Float5 { get; set; }


        public DateTime Date1 { get; set; }
        public DateTime Date2 { get; set; }
        public DateTime Date3 { get; set; }
        public DateTime Date4 { get; set; }
        public DateTime Date5 { get; set; }
        public DateTime Date6 { get; set; }
        public DateTime Date7 { get; set; }
        public DateTime Date8 { get; set; }
        public int PageCount { get; set; }
        #endregion
        public virtual bool Execute(object id)
        { return true; }
        public virtual object Init() { return new bool(); }
        public event EventHandler Save;
        public event EventHandler SavePage;
        public event EventHandler SaveImage;

        private void UcAspControl_Load(object sender, EventArgs e)
        {
            
            txtContent.Left = 6;
            txtContent.Top = 86;
            this.Controls.Add(txtContent);

            string secrity = UcAsp.Net.Comm.Encrypt.EncodeEncrypt(Security);

            Dictionary<string, string> param = new Dictionary<string, string>();


            string code = HttpWebClient.GetString("http://www.hzspace.com/admin/GetCategories?Security=" + secrity + "&ID=0");
            JsonSerializer serializer = new JsonSerializer();
            JsonReader json = new JsonTextReader(new StringReader(code));
            List<Model.Categories> list = (List<Model.Categories>)serializer.Deserialize(json, typeof(List<Model.Categories>));
            Model.Categories model = new Model.Categories { ID = 0, CategorieName = "请选择分类" };
            list.Insert(0, model);
            cmbCategories.DataSource = list;
            cmbCategories.DisplayMember = "CategorieName";
            cmbCategories.ValueMember = "ID";
            cmbCategories.SelectedValueChanged += new EventHandler(cmbCategories_SelectedIndexChanged);
        }

        void cmbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            Control[] second = this.Controls.Find("second", true);
            if (second.Length > 0)
            {
                Controls.Remove(second[0]);
            }
            Control[] third = this.Controls.Find("third", true);
            if (third.Length > 0)
            {
                Controls.Remove(third[0]);
            }
            if (cmbCategories.SelectedValue.ToString() != "0")
            {
                string secrity = UcAsp.Net.Comm.Encrypt.EncodeEncrypt(Security);
                string code = HttpWebClient.GetString("http://www.hzspace.com/admin/GetCategories?Security=" + secrity + "&ID=" + cmbCategories.SelectedValue + "");
                JsonSerializer serializer = new JsonSerializer();
                JsonReader json = new JsonTextReader(new StringReader(code));
                List<Model.Categories> list = (List<Model.Categories>)serializer.Deserialize(json, typeof(List<Model.Categories>));
                if (list.Count > 0)
                {
                    Model.Categories model = new Model.Categories { ID = 0, CategorieName = "请选择分类" };
                    list.Insert(0, model);
                    ComboBox cmb = new ComboBox();
                    cmb.Name = "second";
                    cmb.Left = 200;
                    cmb.Top = 6;
                    this.Controls.Add(cmb);
                    cmb.DataSource = list;
                    cmb.DisplayMember = "CategorieName";
                    cmb.ValueMember = "ID";
                    cmb.SelectedValueChanged += new EventHandler(cmb_SelectedIndexChanged);

                }
            }
        }

        void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            Control[] third = this.Controls.Find("third", true);
            if (third.Length > 0)
            {
                Controls.Remove(third[0]);
            }
            ComboBox cmb = (ComboBox)sender;
            if (cmb.SelectedValue.ToString() != "0")
            {
                string secrity = UcAsp.Net.Comm.Encrypt.EncodeEncrypt(Security);
                string code = HttpWebClient.GetString("http://www.hzspace.com/admin/GetCategories?Security=" + secrity + "&ID=" + cmb.SelectedValue + "");
                JsonSerializer serializer = new JsonSerializer();
                JsonReader json = new JsonTextReader(new StringReader(code));
                List<Model.Categories> list = (List<Model.Categories>)serializer.Deserialize(json, typeof(List<Model.Categories>));
                if (list.Count > 0)
                {
                    Model.Categories model = new Model.Categories { ID = 0, CategorieName = "请选择分类" };
                    list.Insert(0, model);
                    ComboBox cmbn = new ComboBox();
                    cmbn.Name = "third";

                    cmbn.Left = 340;
                    cmbn.Top = 6;
                    //cmbn.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
                    this.Controls.Add(cmbn);
                    cmbn.DataSource = list;
                    cmbn.DisplayMember = "CategorieName";
                    cmbn.ValueMember = "ID";
                    //cmb.SelectedIndexChanged += new EventHandler(cmbCategories_SelectedIndexChanged);
                }
            }
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int categories = 0;
            Control[] third = this.Controls.Find("third", true);
            if (third.Length > 0)
            {
                categories = int.Parse(((ComboBox)third[0]).SelectedValue.ToString());

            }
            else
            {
                Control[] second = this.Controls.Find("second", true);
                if (second.Length > 0)
                {
                    categories = int.Parse(((ComboBox)second[0]).SelectedValue.ToString());
                }
                else
                {
                    categories = int.Parse(cmbCategories.SelectedValue.ToString());
                }
            }
            Title = txtTitle.Text;
            Image = @"<(?i)img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<img>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
            ArticleContent = txtContent.BodyHtml;
            CategorieID = categories;
            if (!chbAgain.Checked)
            {
                ID = 0;
                if (Save != null)
                {
                    Save(this, e);
                }
            }
            else
            {
                if (SavePage != null)
                {
                    SavePage(this, e);
                }
            }
        }

    }
}
